package com.ruoyi.activity.service.impl;

import java.util.List;

import com.ruoyi.activity.mapper.EvaluationMapper;
import com.ruoyi.activity.model.domain.Evaluation;
import com.ruoyi.activity.model.domain.Work;
import com.ruoyi.client.model.dto.ApiExpectEvaluationRequest;
import com.ruoyi.client.vo.ApiExpectEvaluationVO;
import com.ruoyi.activity.service.IWorkService;
import com.ruoyi.common.exception.base.BaseException;
import org.json.JSONArray;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.activity.service.IEvaluationService;

/**
 * 作品评价信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-28
 */
@Service
public class EvaluationServiceImpl implements IEvaluationService {
    @Autowired
    private EvaluationMapper evaluationMapper;

    @Autowired
    private IWorkService workService;

    /**
     * 查询作品评价信息
     *
     * @param id 作品评价信息主键
     * @return 作品评价信息
     */
    @Override
    public Evaluation selectEvaluationById(Long id) {
        return evaluationMapper.selectEvaluationById(id);
    }

    /**
     * 查询作品评价信息列表
     *
     * @param evaluation 作品评价信息
     * @return 作品评价信息
     */
    @Override
    public List<Evaluation> selectEvaluationList(Evaluation evaluation) {
        return evaluationMapper.selectEvaluationList(evaluation);
    }

    /**
     * 新增作品评价信息
     *
     * @param evaluation 作品评价信息
     * @return 结果
     */
    @Override
    public int insertEvaluation(Evaluation evaluation) {
        Long workId = evaluation.getWorkId();
        Work work = workService.selectWorkById(workId);
        String classroomRecording = work.getClassroomRecording();
        JSONArray jsonArray = new JSONArray(classroomRecording);
        long length = jsonArray.length();
        evaluation.setVideoMaterials(length);
        String lessonPlan = work.getLessonPlan();
        jsonArray = new JSONArray(lessonPlan);
        length = jsonArray.length();
        evaluation.setLessonPlan(length);
        return evaluationMapper.insertEvaluation(evaluation);
    }

    /**
     * 修改作品评价信息
     *
     * @param evaluation 作品评价信息
     * @return 结果
     */
    @Override
    public int updateEvaluation(Evaluation evaluation) {
        return evaluationMapper.updateEvaluation(evaluation);
    }

    /**
     * 批量删除作品评价信息
     *
     * @param ids 需要删除的作品评价信息主键
     * @return 结果
     */
    @Override
    public int deleteEvaluationByIds(Long[] ids) {
        return evaluationMapper.deleteEvaluationByIds(ids);
    }

    /**
     * 删除作品评价信息信息
     *
     * @param id 作品评价信息主键
     * @return 结果
     */
    @Override
    public int deleteEvaluationById(Long id) {
        return evaluationMapper.deleteEvaluationById(id);
    }

    @Override
    public boolean reviewWork(ApiExpectEvaluationRequest apiExpectEvaluationRequest) {
        Long workId = apiExpectEvaluationRequest.getWorkId();
        if (workId == null) {
            throw new BaseException("参数错误");
        }
        Evaluation evaluation = evaluationMapper.selectEvaluationByWorkId(workId);

        BeanUtils.copyProperties(apiExpectEvaluationRequest, evaluation);
        evaluation.setScore(apiExpectEvaluationRequest.getSumScore());

        int result = evaluationMapper.updateEvaluation(evaluation);
        if (result <= 0) {
            throw new BaseException("更新失败");
        }
        return true;
    }

    @Override
    public ApiExpectEvaluationVO getApiExceptEvaluationVo(Evaluation evaluation) {
        ApiExpectEvaluationVO apiExpectEvaluationVo = new ApiExpectEvaluationVO();
        BeanUtils.copyProperties(evaluation, apiExpectEvaluationVo);
        return apiExpectEvaluationVo;
    }
}
